﻿package com.petermacko.utils {
	public class StringUtil
	{
		public static const IS_NUMERIC_REGEX:RegExp = /^[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$/;
		public static const QUOTE_REGEX:RegExp = /[\\"\r\n]/g;
		public static const REMOVE_HTML_REGEX:RegExp = /<[^>]*>/g;
		
		//Determines whether the specified string contains text.
		public static function hasText(_str:String):Boolean {
			var str:String = removeExtraWhitespace(_str);
			return !!str.length;
		}

		//Returns <code>true</code> if the specified string is a single space, tab, carriage return, newline, or formfeed character.
		public static function isWhitespace(char:String):Boolean {
	        switch(char) {
	            case " ":
	            case "\t":
	            case "\r":
	            case "\n":
	            case "\f":
	                return true;
	            default:
	                return false;
	        }
	    }

		//Determines whether the specified string is numeric.
		public static function isNumeric(_str:String):Boolean {
			if (_str == null) {
				return false;
			}
			return IS_NUMERIC_REGEX.test(_str);
		}

		//Remove's all < and > based tags from a string
		public static function stripTags(_str:String):String {
			if (_str == null) {
				return "";
			}
			return _str.replace(/<\/?[^>]+>/igm, "");
		}
		
		//Escapes all of the characters in a string to create a friendly "quotable" sting
		public static function quote(_str:String):String {
			return '"'+ _str.replace(QUOTE_REGEX, _quote) +'"';
		}
		
		//Removes whitespace from the front and the end of the specified string.
		public static function trim(input:String):String {
			return StringUtil.ltrim(StringUtil.rtrim(input));
		}
		
		//Removes whitespace from the front of the specified string.
		public static function ltrim(input:String):String {
			var s:Number = input.length;
			for(var i:Number = 0; i < s; i++) {
				if(input.charCodeAt(i) > 32) {
					return input.substring(i);
				}
			}
			return "";
		}
		
		//Removes whitespace from the end of the specified string.
		public static function rtrim(input:String):String {
			var s:Number = input.length;
			for(var i:Number = s; i > 0; i--) {
				if(input.charCodeAt(i - 1) > 32) {
					return input.substring(0, i);
				}
			}
			return "";
		}
		
		//Removes whitespace from the front (left-side) of the specified string.
		public static function trimLeft(_str:String):String {
			if (_str == null) {
				return "";
			}
			return _str.replace(/^\s+/, "");
		}
		
		//Removes whitespace from the end (right-side) of the specified string.
		public static function trimRight(_str:String):String {
			if (_str == null) {
				return "";
			}
			return _str.replace(/\s+$/, "");
		}
		
		//Replaces all instances of the replace string in the input string with the replaceWith string.
		public static function replace(ip:String,r:String,rW:String):String {
			var sb:String=new String();
			var f:Boolean=false;
			var sL:Number=ip.length;
			var rL:Number=r.length;
			
			for(var i:Number=0;i<sL;i++) {
				if(ip.charAt(i)==r.charAt(0)) {   
					f=true;
					for(var j:Number=0;j<rL;j++) {
						if(!(ip.charAt(i+j)==r.charAt(j))) {
							f=false;
							break;
						}
					}
					if(f) {
						sb += rW;
						i = i+(rL-1);
						continue;
					}
				}
				sb += ip.charAt(i);
			}
			return sb;
		}
		
		//Removes all whitespace characters from the beginning and end of each element in an Array, where the Array is stored as a String.
		public static function trimArrayElements(val:String,delimiter:String):String {
	        if (val != "" && val != null) {
	            var its:Array = val.split(delimiter);
	            var l:int = its.length;
	            for (var i:int = 0; i < l; i++) {
	                its[i] = StringUtil.trim(its[i]);
	            }
	            if (l > 0) {
	                val = its.join(delimiter);
	            }
	        }
	        return val;
	    }
		
		//Removes all instances of the remove string in the input string.
		public static function remove(input:String, remove:String):String {
			return StringUtil.replace(input,remove,"");
		}
		
		//Removes extraneous whitespace (extra spaces, tabs, line breaks, etc) from the specified string.
		public static function removeExtraWhitespace(_str:String):String {
			if (_str == null) {
				return "";
			}
			var str:String = trim(_str);
			return str.replace(/\s+/g, " ");
		}
		
		//Removes all xml / html tags and returns the a non-marked up String
		public static function removeHTML(src:String):String {
			return src.replace(REMOVE_HTML_REGEX,"");
		}

		//Determins the number of words in a string.
		public static function wordCount(_str:String):uint {
			if (_str == null) {
				return 0;
			}
			return _str.match(/\b\w+\b/g).length;
		}
	}
}